package com.glsc.ngateway.platform.controller.other;


import com.glsc.ngateway.common.base.domain.mysql.gateway.other.SystemLicenseExpire;
import com.glsc.ngateway.common.api.platform.resp.PlatformResponse;
import com.glsc.ngateway.common.api.common.utils.PageUtil;
import com.glsc.ngateway.common.api.common.ex.Result;
import com.glsc.ngateway.platform.service.other.SystemLicenseExpireService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * @author zhouzp
 * @date 2020/8/19
 * 系统license管理
 */
@RestController
@RequestMapping("/api/systemlicense")
@Api(value = "系统license管理", tags = {"系统license管理"})
public class SystemLicenseExpireController {

    private static Logger logger = LoggerFactory.getLogger(SystemLicenseExpireController.class);

    @Resource
    private SystemLicenseExpireService systemLicenseExpireService;

    @PostMapping("/create")
    @ApiOperation(value = "新增", notes = "新增")
    public PlatformResponse<Void> create(@RequestBody SystemLicenseExpire licenseExpire) {
        logger.info("新增，" +licenseExpire.toString());
        try {
            SystemLicenseExpire licenseExpireDB= systemLicenseExpireService.save(licenseExpire);

            return PlatformResponse.successMsg("新增" + licenseExpire.getSystemName()+ "授权日期成功");
        } catch (Exception e) {
            logger.error("Error", e);
            logger.error("Error" + Arrays.toString(e.getStackTrace()));
            return PlatformResponse.failedMsg("新增" + licenseExpire.getSystemName()+ "授权日期保存失败:" + e.getMessage());
        }
    }

    @PostMapping("/update")
    @ApiOperation(value = "更新系统License信息", notes = "更新系统License信息")
    public PlatformResponse<Void> update(@RequestBody SystemLicenseExpire licenseExpire) {
        logger.info("更新系统License信息：" +licenseExpire.toString());
        try {

            SystemLicenseExpire licenseExpireDB= systemLicenseExpireService.save(licenseExpire);

            return PlatformResponse.successMsg("更新系统License " + licenseExpire.getSystemName()+ "授权日期成功");
        } catch (Exception e) {
            logger.error("Error", e);
            logger.error("Error" + Arrays.toString(e.getStackTrace()));
            return PlatformResponse.failedMsg( "更新系统License" + licenseExpire.getSystemName()+ "授权日期保存失败:" + e.getMessage());
        }
    }

    @PostMapping("/delete")
    @ApiOperation(value = "删除系统License信息", notes = "删除系统License信息")
    public PlatformResponse<Void> delete(@ApiParam(required = true, value = "投资信息ID") @RequestBody SystemLicenseExpire licenseExpire) {
        try {
            systemLicenseExpireService.deleteById(licenseExpire.getSystemId());

            return PlatformResponse.successMsg("删除系统License信息成功");
        } catch (Exception e) {
            return PlatformResponse.failedMsg("删除系统License信息" + licenseExpire.getSystemName()+ "失败："+ e.getMessage());
        }
    }

    @GetMapping("/findbyid")
    @ApiOperation(value = "根据ID查询系统授权信息", notes = "根据ID查询系统授权信息")
    public Result<SystemLicenseExpire> findById(@ApiParam(required = true, value = "系统ID") @RequestParam Integer systemId) {
        try {
            SystemLicenseExpire licenseExpire= systemLicenseExpireService.findById(systemId);
            return Result.successData(licenseExpire);
        } catch (Exception e) {
            logger.error("Error", e);
            logger.error("根据ID查询系统授权信息失败："+e.getMessage()+","+e.getStackTrace());
            return Result.failData("根据ID查询系统授权信息失败："+e.getMessage());
        }
    }

    @GetMapping("/listpage")
    @ApiOperation(value = "查询系统授权信息列表", notes = "查询系统授权信息列表")
    public Result<Page<SystemLicenseExpire>> listpage(@ApiParam(required = false, value = "系统名称") @RequestParam(required = false) String systemName,
                                        @ApiParam(value = "页码从1开始") @RequestParam(required = false) Integer pageNo,
                                        @ApiParam(value = "每页显示条数 默认10") @RequestParam(required = false) Integer pageSize ){
        logger.info("listpage:systemName="+systemName);
        Map<String, Object> result = new HashMap<>();
        Map<String, Object> param = new HashMap<>();

        if (StringUtils.isNotBlank(systemName)) {
            param.put("LIKE_systemName", systemName);
        }

        pageNo= PageUtil.initPageNo(pageNo);
        pageSize=PageUtil.initPageSize(pageSize);

        Page<SystemLicenseExpire> page = systemLicenseExpireService.findPage(pageNo,pageSize,param);
        return Result.successData(page);
    }


}
